package com.sg.service.biz.bff.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.bff.req.*;
import com.sg.dto.biz.bff.res.*;
import com.wicket.okrbff.common.dto.PublicCacheInfoDto;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.common.redis.PublicInfoForRedis;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
@BanAuto
public class NbUser {

    @Resource
    private RedisUtil redisClient;

    /**
     * code:generateToken
     * name:登录成功生成Token
     * desc:undefined
     **/
    @BanAuto
    public CreateTokenRespDto createToken(CreateTokenReqDto reqDto) {
        CreateTokenRespDto retData = new CreateTokenRespDto();
        retData.setToken(CommonFunctionHelper.generateToken());
        return retData;
    }

    /**
     * code:checkCaptchaValid
     * name:D1-检测验证码是否有效（特殊方法）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "D1-检测验证码是否有效（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CheckCaptchaValidRespDto checkCaptchaValid(CheckCaptchaValidReqDto reqDto) {
        CheckCaptchaValidRespDto retData = new CheckCaptchaValidRespDto();
        retData.setIsInvalid("FALSE");
        return retData;
    }

    /**
     * code:clearTokenFromRedis
     * name:Redis清空用户Token
     * desc:undefined
     **/
    @BanAuto
    public ClearTokenFromRedisRespDto clearTokenFromRedis(ClearTokenFromRedisReqDto reqDto) {
        String token = (String) redisClient.get("token-" + reqDto.getOriginalRoleMemberId());
        if (token == null || token.isEmpty()) {
            return new ClearTokenFromRedisRespDto();
        }
        redisClient.delete(token);
        redisClient.delete("token-" + reqDto.getOriginalRoleMemberId());
        return new ClearTokenFromRedisRespDto();
    }

    /**
     * code:putPublicInfoToRedis
     * name:公共信息存储Redis
     * desc:undefined
     **/
    @BanAuto
    public PutPublicInfoToRedisRespDto putPublicInfoToRedis(PutPublicInfoToRedisReqDto reqDto) {
        try {
            if (reqDto != null) {
                if (reqDto.getInductionUnitTypeCode() != null && reqDto.getInductionUnitTypeCode().equals("DEPARTMENT")) {
                    reqDto.setDeptId(reqDto.getDeptId());
                }
                PublicInfoForRedis info = BeanUtil.toBean(reqDto, PublicInfoForRedis.class);
                Object jo = JSONArray.toJSON(info);
                redisClient.set("token-" + info.getOriginalRoleMemberId(), reqDto.getToken());
                redisClient.set(reqDto.getToken(), jo.toString());
            }
        } catch (Exception ex) {
        }
        return new PutPublicInfoToRedisRespDto();
    }

    /**
     * code:batchClearTokenFromRedis
     * name:Redis批量清空用户Token
     * desc:undefined
     **/
    @BanAuto
    public BatchClearTokenFromRedisRespDto batchClearTokenFromRedis(BatchClearTokenFromRedisReqDto reqDto) {
        if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
            return new BatchClearTokenFromRedisRespDto();
        }
        for (String member : reqDto.getBeginMemberList()) {
            ClearTokenFromRedisReqDto req = new ClearTokenFromRedisReqDto();
            req.setOriginalRoleMemberId(member);
            clearTokenFromRedis(req);
        }
        return new BatchClearTokenFromRedisRespDto();
    }

    /**
     * code:batchClearUserInfoFromRedis
     * name:Redis批量清空用户信息
     * desc:undefined
     **/
    @BanAuto
    public BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedis(BatchClearUserInfoFromRedisReqDto reqDto) {
        if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
            return new BatchClearUserInfoFromRedisRespDto();
        }
        for (String member : reqDto.getBeginMemberList()) {
            redisClient.delete("userbase-" + member);
            redisClient.delete("inductionInfo-" + member);
        }
        return new BatchClearUserInfoFromRedisRespDto();
    }

    /**
     * code:clearUserInfoFromRedis
     * name:Redis清空用户信息
     * desc:undefined
     **/
    @BanAuto
    public ClearUserInfoFromRedisRespDto clearUserInfoFromRedis(ClearUserInfoFromRedisReqDto reqDto) {
        if (reqDto.getOriginalRoleMemberId() == null || reqDto.getOriginalRoleMemberId().isEmpty()) {
            return new ClearUserInfoFromRedisRespDto();
        }
        redisClient.delete("userbase-" + reqDto.getOriginalRoleMemberId());
        redisClient.delete("inductionInfo-" + reqDto.getOriginalRoleMemberId());
        return new ClearUserInfoFromRedisRespDto();
    }

    /**
     * code:changePublicInfoFromRedis
     * name:Redis切换公共信息（后端）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "Redis切换公共信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ChangePublicInfoFromRedisRespDto changePublicInfoFromRedis(ChangePublicInfoFromRedisReqDto reqDto) {
        String token = (String) redisClient.get("登录令牌" + ":" + "token-" + CommonFunctionHelper.getBaseInfoFromLocal().getSpecOriginalRoleMemberId());
        PublicCacheInfoDto info = CommonFunctionHelper.getPublicInfoFromRedis(redisClient, token);
        if (info == null) {
            return new ChangePublicInfoFromRedisRespDto();
        }
        info.setInductionRecordId(reqDto.getInductionRecordId());
        Object jo = JSONArray.toJSON(info);
        redisClient.set("登录令牌" + ":" + token, jo.toString());
        return new ChangePublicInfoFromRedisRespDto();
    }

    //手工接入方法
}
